package thebesttaskmanager.tstore;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import java.util.ArrayList;

public class TStore {
  private String fileName;

  /**
   * Constructor for TStore object
   * @param fileName Name of file
   */
  public TStore(String fileName) {
    this.fileName = fileName;
  }
  
/**
 * Reads file specified by object variable fileName and returns data as a String ArrayList
 * @return
 */
  public ArrayList<String> readFile() {
    ArrayList<String> data = new ArrayList<String>();

    try {
      String line;
      BufferedReader reader = new BufferedReader(new FileReader(fileName));
      
      while ((line = reader.readLine()) !=null) {
	data.add(line);
      }
      reader.close();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return data;
  }
  /**
   * Writes a ArrayList<String> to file specified by object data fileName.
   * @param lines
   */
  public void writeFile(ArrayList<String> lines) {
    try {
      BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
    
      for (String line: lines) {
	writer.write(line);
	writer.newLine();
      }
      writer.close();
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}